﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace reportsDesktop
{
    public partial class frmSetting : Form
    {
        Point oldPoint;
        bool mouseDown;

        static frmMain main;

        static WebBrowser wb;

        static frmSetting instence;

        private frmSetting()
        {
            InitializeComponent();
        }

        public void SaveForm()
        {
            if (wb.Document == null)
                return;

            Properties.Settings.Default.form = new StreamReader(wb.DocumentStream, Encoding.Default).ReadToEnd();
            Properties.Settings.Default.formUrl = wb.Url.ToString();
            SaveSettingAndShowSuccess();
            
        }

        public static frmSetting CreateInstence(frmMain main2)
        {
            if (instence == null)
                instence = new frmSetting();

            wb = main2.webBrowser1;
            main = main2;

            return instence;
        }

        private void frmSetting_Paint(object sender, PaintEventArgs e)
        {
            RoundFormPainter.DrawFormBodder2(sender);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                oldPoint = e.Location;
                mouseDown = true;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                this.Left += (e.X - oldPoint.X);
                this.Top += (e.Y - oldPoint.Y);
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void btnMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            RoundFormPainter.SetWindowRegion2(sender);

            LoadLoginSetting();
            chkAutoLogin.CheckedChanged += new EventHandler(chkAutoLogin_CheckedChanged);
            chkShowInstr.CheckedChanged += new EventHandler(chkShowInstr_CheckedChanged);
            chkRemember.CheckedChanged += new EventHandler(chkRemember_CheckedChanged);
            LoadUrlSetting();
            LoadTimeCount();
            
        }

        private void LoadLoginSetting()
        {
            chkRemember.Checked = Properties.Settings.Default.remember;
            chkAutoLogin.Checked = Properties.Settings.Default.autoLogin;
            chkShowInstr.Checked = Properties.Settings.Default.showInstr;
        }

        private void LoadUrlSetting()
        {
            txtHome.Text = Properties.Settings.Default.home;
            txtUrl1.Text = Properties.Settings.Default.url1;
            txtUrl2.Text = Properties.Settings.Default.url2;
            txtUrl2.Text = Properties.Settings.Default.url2;
        }

        private void LoadTimeCount()
        {
            txtTotalTime.Text = Properties.Settings.Default.totalTime.ToString();
            txtUsedHour.Text = Properties.Settings.Default.usedHour.ToString();
            txtUsedMin.Text = Properties.Settings.Default.usedMin.ToString();
            chkUseTimeCount.Checked = Properties.Settings.Default.useTimeCount;
        }




        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                wb.Document.GetElementById(cmbLogin.SelectedValue.ToString()).InvokeMember("click");
            }
            catch (Exception ee)
            {
                //todo:登陆
                HtmlElement he = wb.Document.GetElementById("");
                while (he.TagName != "form" && he != null && he.TagName != "FORM")
                {
                    he = he.Parent;
                }

                he.InvokeMember("submit");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            lblSuccess.Visible = false;
            timer1.Stop();
        }
        private void SaveSettingAndShowSuccess()
        {
            Properties.Settings.Default.Save();

            lblSuccess.Visible = true;
            lblSuccess.BringToFront();
            timer1.Start();
        }

        private void chkAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (cmbLogin.Items.Count < 1)
            {
                MessageBox.Show("没找到登陆按钮，自动登陆设置无效！");
                return;
            }
            Properties.Settings.Default.autoLogin = chkAutoLogin.Checked;
            Properties.Settings.Default.loginControl = cmbLogin.SelectedValue.ToString();
            SaveSettingAndShowSuccess();
        }

        private void cmbLogin_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                wb.Document.GetElementById(cmbLogin.SelectedValue.ToString()).SetAttribute("value", "景风");

                Properties.Settings.Default.loginControl = cmbLogin.SelectedValue.ToString();
                
                SaveSettingAndShowSuccess();
            }
            catch (Exception ee) { }
        }

        private void txtHome_Leave(object sender, EventArgs e)
        {
                Properties.Settings.Default.home = txtHome.Text.Trim();
                main.UpdateCmbUrl();
                SaveSettingAndShowSuccess();
        }

        private void txtUrl1_Leave(object sender, EventArgs e)
        {
                Properties.Settings.Default.url1 = txtUrl1.Text.Trim();
                main.UpdateCmbUrl();
                SaveSettingAndShowSuccess();
        }

        private void txtUrl2_Leave(object sender, EventArgs e)
        {
                Properties.Settings.Default.url2 = txtUrl2.Text.Trim();
                main.UpdateCmbUrl();
                SaveSettingAndShowSuccess();
        }

        private void txtUrl3_Leave(object sender, EventArgs e)
        {
                Properties.Settings.Default.url3 = txtUrl3.Text.Trim();
                main.UpdateCmbUrl();
                SaveSettingAndShowSuccess();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            txtTotalTime.ReadOnly = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            txtUsedHour.ReadOnly = false;
            txtUsedMin.ReadOnly = false;
        }

        private void txtTotalTime_Leave(object sender, EventArgs e)
        {
            if (txtTotalTime.ReadOnly == false)
            {
                Properties.Settings.Default.totalTime = int.Parse(txtTotalTime.Text);
                txtTotalTime.ReadOnly = true;
                SaveSettingAndShowSuccess();
            }
            
        }

        private void txtTotalTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar < '０' || e.KeyChar > '９') && e.KeyChar != '\b';
            //e.Handled 在为true的时候，文本框中什么都不输入也不显示。当所输入的字符统统不在允许输入的字符内时，e.Handled 就为true。
            if (e.KeyChar >= '０' && e.KeyChar <= '９')
            {
                e.KeyChar = quanbianban(e.KeyChar);
            }
        }
        //全角数字变半角数字
        public char quanbianban(char quan)
        {
            switch (quan)
            {

                case '０':
                    return '0';

                case '１':
                    return '1';

                case '２':
                    return '2';

                case '３':
                    return '3';

                case '４':
                    return '4';

                case '５':
                    return '5';

                case '６':
                    return '6';

                case '７':
                    return '7';

                case '８':
                    return '8';

                case '９':
                    return '9';

                default:
                    return '9';

            }
        }

        private void txtUsedHour_Leave(object sender, EventArgs e)
        {
            if (txtUsedHour.ReadOnly == false)
            {
                Properties.Settings.Default.usedHour = int.Parse(txtUsedHour.Text);
                txtUsedHour.ReadOnly = true;
                SaveSettingAndShowSuccess();
            }
        }

        private void txtUsedMin_Leave(object sender, EventArgs e)
        {
            if (txtUsedMin.ReadOnly == false)
            {
                Properties.Settings.Default.usedMin = int.Parse(txtUsedMin.Text);
                txtUsedMin.ReadOnly = true;
                SaveSettingAndShowSuccess();
            }
        }
        
        private void chkUseTimeCount_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.useTimeCount = chkUseTimeCount.Checked;
            SaveSettingAndShowSuccess();
            if (chkUseTimeCount.Checked == true)
                main.timer1.Start();
            else
                main.timer1.Stop();
        }

        private void btnLoginSetting_Click(object sender, EventArgs e)
        {
            pnlLoginSetting.BringToFront();
        }


        private void btnUrlSetting_Click(object sender, EventArgs e)
        {
            pnlUrlSetting.BringToFront();
        }

        private void btnTimeCount_Click(object sender, EventArgs e)
        {
            pnlTime.BringToFront();
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            pnlAbout.BringToFront();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            SaveForm();
        }

        private void chkShowInstr_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.showInstr = chkShowInstr.Checked;
            SaveSettingAndShowSuccess();
        }
        private void chkRemember_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.remember = chkRemember.Checked;
            SaveSettingAndShowSuccess();
        }
        
    }
}
